<?php

namespace App\Http\Controllers\Customer;


use App\Http\Controllers\Admin\Examine\Record;
use App\Http\Controllers\Controller;
use App\Http\Requests\service\CompensateRequest;
use App\Models\service\CompensateModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class Compensate extends Controller
{
    use CompensateRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $auth = $request->get('auth');
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $take_order_sn = $request->input('take_order_sn', '');
        $status = $request->input('status', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');


        $query = CompensateModel::query()->with(['actual_currency', 'currency'])->withWhereHas('take_order', function ($query) use ($take_order_sn) {
            if($take_order_sn) {
                $query->where('sn', $take_order_sn);
            }
        })->where('customer_id', $auth->id);

        if($status){
            $query = $query->where('status', $status);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $currency_symbol = $item->currency ? $item->currency->symbol : '';
            $actual_currency_symbol = $item->actual_currency ? $item->actual_currency->symbol : '';
            $res = [
                'id' => $item->id,
                'take_order_sn' => $item->take_order->sn ?? '',
                'amount' => $currency_symbol . $item->amount,
                'actual_amount' => $actual_currency_symbol . $item->actual_amount,
                'remarks' => $item->remarks,
                'compensate_remarks' => $item->compensate_remarks,
                'status' => $item->status,
                'status_text' => CompensateModel::STATUS[$item->status],
                'flight_date' => $item->flight_date,
                'start_port_code' => $item->start_port_code,
                'arrive_port_code' => $item->arrive_port_code,
                'number' => $item->number,
                'weight' => $item->weight,
                'created_at' => $item->created_at->toDateTimeString(),

                'currency_id' => $item->currency_id,
                'pod_file' => $item->pod_file,
                'letter_file' => $item->letter_file,
                'lose_invoice' => $item->lose_invoice,
                'whole_invoice' => $item->whole_invoice,
                'lose_list' => $item->lose_list,
                'whole_list' => $item->whole_list,
                'pic_file' => $item->pic_file ? json_decode($item->pic_file) : [],
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }


    /**
     * Notes:详情
     * User: xiangzi
     * Date: 2024/9/20
     * Time: 11:30
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request){
        $id = intval($request->input('id', 0));
        $model = CompensateModel::with(['actual_currency', 'currency', 'take_order'])->find($id);
        if(!$model){
            return $this->error('数据不存在');
        }

        $currency_symbol = $model->currency ? $model->currency->symbol : '';
        $actual_currency_symbol = $model->actual_currency ? $model->actual_currency->symbol : '';

        $return = [
            'compensate_info' => [
                'take_order_sn' => $model->take_order ? $model->take_order->sn : '',
                'created_at' => $model->created_at->toDateTimeString(),
                'amount' => $currency_symbol . $model->amount,
                'remarks' => $model->remarks,
                'letter_file' => $model->letter_file,
                'pod_file' => $model->pod_file,
                'lose_invoice' => $model->lose_invoice,
                'whole_invoice' => $model->whole_invoice,
                'lose_list' => $model->lose_list,
                'whole_list' => $model->whole_list,
                'status' => $model->status,
                'status_text' => CompensateModel::STATUS[$model->status],
                'pic_file' => $model->pic_file ? json_decode($model->pic_file, true) : []
            ],

            'actual_info' => [
                'actual_amount' => $actual_currency_symbol . $model->actual_amount,
                'compensate_remarks' => $model->compensate_remarks,
                'compensate_time' => $model->compensate_time,
            ]
        ];
        return $this->success($return);
    }

    public function apply(Request $request) {

        $id = intval($request->input('id', 0));
        $params = $request->all();

        $compensate = CompensateModel::find($id);

        if(!$compensate){
            return $this->error('数据不存在');
        }

        DB::beginTransaction();
        try {

            $compensate->amount = $params['amount'];
            $compensate->currency_id = $params['currency_id'];
            $compensate->pod_file = $params['pod_file'];
            $compensate->letter_file = $params['letter_file'];
            $compensate->lose_invoice = $params['lose_invoice'];
            $compensate->whole_invoice = $params['whole_invoice'];
            $compensate->lose_list = $params['lose_list'];
            $compensate->whole_list = $params['whole_list'];
            $compensate->remarks = $params['remarks'] ?? '';
            $compensate->pic_file = $params['pic_file'] ? json_encode($params['pic_file']) : '';
            $compensate->examine_record_id = (new Record())->add(2);
            $compensate->status = 1;
            $compensate->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();


    }


    // 补充信息
    public function info(Request $request) {
        $id = intval($request->input('id', 0));
        $params = $request->all();

        $compensate = CompensateModel::find($id);

        if(!$compensate){
            return $this->error('数据不存在');
        }

        $compensate->number = $params['number'] ??  0;
        $compensate->weight = $params['weight'] ?? 0;
        $compensate->save();

        return $this->success();
    }




}
